﻿using Ruici.Web.Services.ArticleService;
using Ruici.Web.Services.CRS_Service;
using Ruici.Web.Services.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;

namespace Ruici.Web.Services
{
    public class DocInfoService
    {
        private CRS_ServiceSoapClient crsSoupService = new CRS_ServiceSoapClient("CRS_ServiceSoap");
        private articleSoapClient articleSoap = new articleSoapClient("articleSoap");
        private CRS_ServiceSoapClient crsService = new CRS_ServiceSoapClient();
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="channel_name">渠道名称</param>
        /// <param name="category_id">类别Id</param>
        /// <param name="page_size">页码</param>
        /// <param name="page_index"></param>
        /// <param name="keys">关键词</param>
        /// <param name="filter">筛选项</param>
        /// <param name="totalcount">总条数</param>
        /// <returns></returns>
        public List<ArticleResponse> GetArticleList(string channel_name, int category_id, int page_size, int page_index, string keys,string filter, out int totalcount)
        {
            DataTable articlemodel = articleSoap.get_article_list(channel_name, category_id, page_size, page_index, keys, filter, out totalcount);
            List<ArticleResponse> articleResponseList = new List<ArticleResponse>();
            foreach (DataRow item in articlemodel.Rows)
            {
                ArticleResponse articleResponse = new ArticleResponse();
                articleResponse.id = Convert.ToInt32(item["id"].ToString());
                articleResponse.channel_id = Convert.ToInt32(item["channel_id"].ToString());
                articleResponse.category_id = Convert.ToInt32(item["category_id"].ToString());
                articleResponse.call_index = item["call_index"].ToString();
                articleResponse.title = item["title"].ToString();
                articleResponse.link_url = item["link_url"].ToString();
                articleResponse.img_url = item["img_url"].ToString();
                articleResponse.seo_title = item["seo_title"].ToString();
                articleResponse.seo_keywords = item["seo_keywords"].ToString();
                articleResponse.seo_description = item["seo_description"].ToString();
                articleResponse.zhaiyao = item["zhaiyao"].ToString();
                articleResponse.content = item["content"].ToString();
                articleResponse.sort_id = Convert.ToInt32(item["sort_id"].ToString());
                articleResponse.click = Convert.ToInt32(item["click"].ToString());
                articleResponse.status = Convert.ToInt32(item["status"].ToString());
                articleResponse.is_msg = Convert.ToInt32(item["is_msg"].ToString());
                articleResponse.is_top = Convert.ToInt32(item["is_top"].ToString());
                articleResponse.is_red = Convert.ToInt32(item["is_red"].ToString());
                articleResponse.is_hot = Convert.ToInt32(item["is_hot"].ToString());
                articleResponse.is_slide = Convert.ToInt32(item["is_slide"].ToString());
                articleResponse.is_sys = Convert.ToInt32(item["is_sys"].ToString());
                articleResponse.user_name = item["user_name"].ToString();
                articleResponse.add_time = Convert.ToDateTime(item["add_time"].ToString()).ToString("yyyy/MM/dd HH:mm");
                
                if (channel_name == "video")
                {
                    articleResponse.doctorID = item["doctorId"].ToString();
                    JEnumerable<JToken> DoctorInfo = GetDoctorListByNumber(Convert.ToInt32(articleResponse.doctorID));

                    if (DoctorInfo.Count() > 0)
                    {
                        foreach (JToken doc in DoctorInfo.ToList())
                        {
                            JProperty docProperty = (JProperty)doc;
                            var docValue = docProperty.First();
                            if (docProperty.Name == "name")
                                articleResponse.doctorName = docValue.ToString();
                            else if (docProperty.Name == "titleName")
                                articleResponse.doctorTitleName = docValue.ToString();
                        }
                    }
                    articleResponse.videoSrc = item["video_src"].ToString();
                }
                else if (channel_name == "guide")
                {
                    string sPDFUrl = ConfigurationManager.AppSettings["PDFUrl"].ToString();
                    articleResponse.fileSrc = sPDFUrl + item["file_src"].ToString();
                }
                articleResponseList.Add(articleResponse);
            }
            return articleResponseList;
        }
        /// <summary>
        /// 城市list Id :城市的code值  name:城市名称
        /// </summary>
        /// <returns></returns>
        public ReturnModel GetCityList()
        {
            ReturnModel citymodel = crsSoupService.getCityList();
            return citymodel;
        }
        /// <summary>
        /// 获取城市
        /// </summary>
        /// <returns></returns>
        public getCityListResponse GetCityListAsync()
        {
            var citymodel = crsSoupService.getCityListAsync();
            return citymodel.Result;
        }

        /// <summary>
        /// 科室list Id :科室的pid值  name:科室名称
        /// </summary>
        /// <returns></returns>
        public List<DeptInfoResponse> GetDeptList()
        {
            var deptModel = crsSoupService.getDeptList();

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<DeptInfoResponse>));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(deptModel.Data.ToString()));
            var obj = (List<DeptInfoResponse>)ser.ReadObject(ms);

            return obj;
        }
        /// <summary>
        /// 获取步骤列表
        /// </summary>
        /// <returns></returns>
        public getDeptListResponse GetDeptListAsync()
        {
            var deptmodel = crsSoupService.getDeptListAsync();
            return deptmodel.Result;
        }

        /// <summary>
        /// 职称list Id :职称的pid值  name:职称名称
        /// </summary>
        /// <returns></returns>
        public List<TitleInfoResponse> GetTitleList()
        {
            List<TitleInfoResponse> titleinfos = new List<TitleInfoResponse>();
            var titlemodel = crsSoupService.getTitleList();

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<TitleInfoResponse>));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(titlemodel.Data.ToString()));
            var obj = (List<TitleInfoResponse>)ser.ReadObject(ms);

            return obj;
        }
        /// <summary>
        /// 获取标题列表
        /// </summary>
        /// <returns></returns>
        public getTitleListResponse GetTitleListAsync()
        {
            var titlemodel = crsSoupService.getTitleListAsync();
            return titlemodel.Result;
        }

        /// <summary>
        /// 诊所列表信息
        /// </summary>
        /// <param name="cityID"></param>
        /// <param name="keyword"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ReturnModel GetOrganizationList(int? cityID, string keyword, int page, int pageSize)
        {
            var organizationList = crsSoupService.getOrganizationList(cityID, keyword, page, pageSize);
            return organizationList;
        }

        /// <summary>
        /// 获取诊所详情
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public ReturnModel GetOrganization(int orgid)
        {
            var organizationModel = crsSoupService.getOrganization(orgid);
            return organizationModel;
        }

        /// <summary>
        /// 医生列表
        /// </summary>
        /// <param name="deptID"></param>
        /// <param name="titleID"></param>
        /// <param name="keyword"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<ActiviteDoctorInfoResponse> GetDoctorList(int? deptID, int? titleID, int pageSize, string keyword, int page)
        {
            List<ActiviteDoctorInfoResponse> activiteDoctorInfoList = new List<ActiviteDoctorInfoResponse>();
            var doctorList = crsSoupService.getDoctorList(deptID, titleID, keyword, "", "", "", page, pageSize);
            if (!string.IsNullOrWhiteSpace(doctorList.Data.ToString()))
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<ActiviteDoctorInfoResponse>));
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(doctorList.Data.ToString()));
                var obj = (List<ActiviteDoctorInfoResponse>)ser.ReadObject(ms);
                return obj;
            }
            else {
                return activiteDoctorInfoList;
            }
        }

        /// <summary>
        /// 医生详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DoctorInfoResponse GetDoctor(string id)
        {
            DoctorInfoResponse obj = new DoctorInfoResponse();
            if (string.IsNullOrWhiteSpace(id))
            {
                return obj;
            }
            else {
                var doctorModel = crsService.getDoctor(Convert.ToInt32(id));
                if (doctorModel.Success)
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DoctorInfoResponse));
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(doctorModel.Data.ToString()));
                    obj = (DoctorInfoResponse)ser.ReadObject(ms);
                }
                return obj;
            }
        }

        /// <summary>
        /// 根据医生id获取医生数据
        /// </summary>
        /// <param name="docIdList">医生id	“1”，“2”</param>
        /// <returns></returns>
        public List<DoctorInfoResponse> GetDoctorListById(string docIdList)
        {
            List<DoctorInfoResponse> doctorInfoList = new List<DoctorInfoResponse>();
            var doctorList = crsSoupService.getDoctorListBy(docIdList);
            if (!(doctorList.Data == null) && !string.IsNullOrEmpty(doctorList.Data.ToString()))
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<DoctorInfoResponse>));
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(doctorList.Data.ToString()));
                var obj = (List<DoctorInfoResponse>)ser.ReadObject(ms);
                return obj;
            }
            else
            {
                return doctorInfoList;
            }
        }

        /// <summary>
        /// 根据医生工号获取医生数据
        /// </summary>
        /// <param name="docNumList">医生id	“M001”，“M002”</param>
        /// <returns></returns>
        public ReturnModel GetDoctorListByNumber(string docNumList)
        {
            var doctorList = crsSoupService.getDoctorListByNumber(docNumList);
            return doctorList;
        }

        public JEnumerable<JToken> GetDoctorListByNumber(int DoctorId)
        {
            JEnumerable<JToken> patinetInforList;
            ReturnModel docList = crsService.getDoctor(DoctorId);
            if (docList.Data != null)
            {
                JObject patientInfoArray = (JObject)JsonConvert.DeserializeObject(docList.Data.ToString());
                patinetInforList = patientInfoArray.Children();
            }
            return patinetInforList;
        }

        /// <summary>
        /// 医生排班信息
        /// </summary>
        /// <param name="docID">医生ID	等于-1的时候为全部医生</param>
        /// <returns></returns>
        public ReturnModelOfListOfDoctorSchedule GetDoctorScheduleBydocID(int docID)
        {
            var doctorSchedule = crsSoupService.GetDoctorScheduleBydocID(docID);
            return doctorSchedule;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="List"></typeparam>
        /// <param name=""></param>
        /// <param name=""></param>
        /// <param name="docID">医生ID	等于-1的时候为全部医生</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        public ReturnModelOfListOfDoctorSchedule GetDoctorScheduleByDate(int docID, DateTime startDate, DateTime endDate)
        {
            var doctorSchedule = crsSoupService.GetDoctorScheduleByDate(docID, startDate, endDate);
            return doctorSchedule;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="List"></typeparam>
        /// <param name=""></param>
        /// <param name=""></param>
        /// <param name="docID">医生ID	等于-1的时候为全部医生</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        public ReturnModelOfListOfString GetDoctorScheduleDates(int docID, DateTime startDate, DateTime endDate)
        {
            var doctorSchedule = crsSoupService.GetDoctorScheduleDates(docID, startDate, endDate);
            return doctorSchedule;
        }


        /// <summary>
        /// 医生排班信息
        /// </summary>
        /// <param name="docID">医生ID	等于-1的时候为全部医生</param>
        /// <returns></returns>
        public ReturnModel GetReservationByCondition(string startime, string endtime, int orgId, int mzId, int docId, string outpatientNo, string reseNo, string patientName, string credType, string credNo, string userNo, string userName, int status)
        {
            var doctorSchedule = crsSoupService.GetReservationByCondition(startime, endtime, orgId, mzId, docId, outpatientNo, reseNo, patientName, credType, credNo, userNo, userName, status);
            return doctorSchedule;
        }

        public ArticleResponse GetArticleInfo(int id)
        {
            ArticleResponse obj = new ArticleResponse();

            var articlemodel = articleSoap.get_article_info(id);
            if (articlemodel != null)
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ArticleResponse));
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(articlemodel.ToString()));
                obj = (ArticleResponse)ser.ReadObject(ms);
            }
            return obj;
        }
    }
}